
package cn.nbt.busManage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"cn.nbt.busManage.controller"})
public class SpringMvcConfig implements WebMvcConfigurer {

    /**
     * 配置跨域
     * @param registry 
     */ @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // Allow CORS requests to all endpoints
                .allowedOrigins("*") // Allow specific origin
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // Allow specific HTTP methods
                .allowedHeaders("*") // Allow specific headers
                .allowCredentials(false) // Allow credentials
                .maxAge(3600); // Max age of the preflight request cache
    }


    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/openlayer/");
        resolver.setSuffix(".html");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    /**
     * 配置静态资源的处理
     * @param configurer 配置处理器
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();        //对静态资源的请求转发到容器缺省的servlet，而不使用DispatcherServlet
    }


    /**
     * 配置视图解析器
     * @param registry 视图控制器注册表
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }



    /**
     * 视图解析器 使用JSP和Jackson作为JSON呈现的默认视图来配置内容
     * @param registry 视图解析器注册表
     */
/*    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.enableContentNegotiation(new MappingJackson2JsonView());
        registry.jsp();
    }*/


    /**
     * 配置静态资源
     * @param registry 静态资源处理器注册表
     */
   @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/lib/bootstrap/css/**")
               .addResourceLocations("webapp/lib/bootstrap/css/");
       registry.addResourceHandler("/lib/bootstrap/js/**")
               .addResourceLocations("webapp/lib/bootstrap/js/");
   }


    //todo:添加拦截器

}
